/*  io.h

    Definitions for Borland low level I/O functions.
*/

/*
 *      C/C++ Run Time Library - Version 1.0
 *
 *      Copyright (c) 2002 by Borland Software Corporation
 *      All Rights Reserved.
 *
 */


#ifndef __IO_H
#define __IO_H

#ifndef ___DEFS_H
#include <_defs.h>
#endif

#include <unistd.h> /* the unix header that contains the normal io.h stuff */
#include <fcntl.h>  /* Most programs need fcntl.h if they bring in io.h */

#ifdef __cplusplus
extern "C" {
#endif

struct ftime				/* for getftime and setftime */
{
	unsigned ft_tsec: 5;	/* two seconds */
	unsigned ft_min: 6;		/* minutes */
	unsigned ft_hour: 5;	/* hours */
	unsigned ft_day: 5;		/* days */
	unsigned ft_month: 4;	/* months */
	unsigned ft_year: 7;	/* year - 1980 */
};

#define SEEK_CUR     1
#define SEEK_END     2
#define SEEK_SET     0

#ifndef MAXPATH
#define MAXPATH   1023
#define MAXDIR     255
#define MAXFILE    255
#define MAXEXT     255
#define MAXDRIVE     3
#endif

_INLFUNC int chsize(int __handle, long __size)
{
	return ftruncate(__handle, (off_t)__size);
}

_INLFUNC int creatnew(const char  *__path, int __mode)
{
	return creat(__path, __mode | O_CREAT | O_EXCL);
}

_INLFUNC int _commit(int __handle)
{
	return fsync(__handle);
}

_INLFUNC int _rtl_open (const char *__path, int __oflags)
{
	return open(__path, __oflags);
}

_INLFUNC int _rtl_close(int __handle)
{
	return close(__handle);
}

_INLFUNC int _rtl_read(int __handle, void *__buf, unsigned __len)
{
	return (int)read(__handle, __buf, (size_t)__len);
}

_INLFUNC int _rtl_write(int __handle, const void *__buf, unsigned __len)
{
	return (int)write(__handle, __buf, (size_t)__len);
}

_INLFUNC int _rtl_creat(const char  *__path, int __mode)
{
	return creat(__path, __mode | O_CREAT | O_EXCL);
}

_INLFUNC int _open(const char *__path, int __oflags)
{
	return open(__path, __oflags);
}

_INLFUNC int _close(int __handle)
{
	return close(__handle);
}

_INLFUNC int _read(int __handle, void  *__buf, unsigned __len)
{
	return (int)read(__handle, __buf, (size_t)__len);
}

_INLFUNC int _write(int __handle, const void  *__buf, unsigned __len)
{
	return (int)write(__handle, __buf, (size_t)__len);
}

_INLFUNC long tell(int __handle)
{
	return (long)lseek(__handle, 0, SEEK_CUR);
}

#pragma option push -w-8057
_INLFUNC int setmode(int __handle, int __amode)
{
	/** Note: the setmode() function isn't used under Linux, since there's
        no differentiation between text and binary files.
	 */
	return 0;
}
#pragma option pop

/** Note: the eof() function cannot be implemented under Linux. Since
	there's no inherent locking applied to files when they are opened,
	another process could be changing the size of the file externally at any
	time. Thus there's no reliable way to determine when one has read the
	last byte of the file.

 */

int creattemp(char  *__path, int __amode);

long filelength(int __handle);

int getftime(int __handle, struct ftime  *__ftimep);

/** Note: setftime() cannot be implemented under Linux.  This is because
    there is no system api availible that can set the date/time of a file
    given its descriptor.  Use utime instead which takes a char * filename.
*/

int sopen (const char *__path, int __access, int __shflag, ... /* unsigned mode */);
#define _sopen sopen


/** Note: Linux implements Advisory not Mandatory locking, so the following
	routines: lock(), locking(), and unlock() will behave differently than
	they do under DOS/Windows.  Most notably, the system file I/O routines:
	open, read, & write don't abide by any locks that might be set.
    Programs that engage in this cooperative type of locking are always
    subject to the effects of other "rogue" processes which are always free
    to do as they wish.

*/
int lock(int __handle, long __offset, long __length);

int locking(int __handle, int __mode, long __length);

int unlock(int __handle, long __offset, long __length);

_INLFUNC long _get_osfhandle(int __handle)
{
	return (long) __handle;
}

#pragma option push -w-8057
_INLFUNC int _open_osfhandle(long __osfhandle, int __oflag)
{
/** Note: __oflag is simply not used
*/
	return (int) __osfhandle;
}
#pragma option pop

#ifdef __cplusplus
} /* "C" */
#endif

#endif  /* __IO_H */

